package com.zeta.eduservice.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.EduAcademy;
import com.zeta.eduservice.entity.EduClasss;
import com.zeta.eduservice.entity.vo.AcademyQuery;
import com.zeta.eduservice.entity.vo.front.ClassListVo;
import com.zeta.eduservice.service.EduClasssService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 班级 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-08-17
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu_classs")
public class EduClasssController {

	@Autowired
	private EduClasssService classsService;
	@ApiOperation("增加学院")
	@PostMapping("")
	public R add(@RequestBody EduClasss classs) {
		boolean flag = classsService.save(classs);
		if(flag)
			return R.ok();
		else
			return R.error();
	}
	@ApiOperation(value = "根据ID删除学院")
	@DeleteMapping("{id}")
	public R remove(@PathVariable String id) {
		boolean flag = classsService.removeById1(id);
		 if(flag){
		        return R.ok();
		    }else{
		        return R.error().message("删除失败");
		    }
	}

	@ApiOperation(value = "根据ID修改学院")
	@PutMapping("")
	public R updateById(
	    @ApiParam(name = "classs", value = "学院对象", required = true)
	    @RequestBody EduClasss classs){
		classsService.updateById(classs);
	    return R.ok();
	}
	
	
	@ApiOperation("查询学院")
	@GetMapping("{id}")
	public R find(@PathVariable String id) {
		ClassListVo classByclassid = classsService.getClassByclassid(id);
		return R.ok().data("classs", classByclassid);
	}
	@ApiOperation("查询所有学院")
	@GetMapping("/findAll")
	public R findAll() {
		List<EduClasss> list = classsService.list(null);
		return R.ok().data("classss", list);
	}
	@ApiOperation(value = "分页查询学院表")
	@GetMapping("{page}/{limit}")
	public R pageList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
			@ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit) {
		Page<EduClasss> pageParam = new Page<>(page, limit);
		classsService.page(pageParam, null);
		List<EduClasss> records = pageParam.getRecords();
		long total = pageParam.getTotal();
		return R.ok().data("total", total).data("rows", records);
	}

	@ApiOperation("条件查询带分页")
	@PostMapping("/pageAcademyCondition/{current}/{limit}")//requestbody必须用 post   required 这个值可以没用
	public R PageCondition(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) AcademyQuery     query) {
		Page<EduClasss> page = new Page<>(current, limit);
		QueryWrapper<EduClasss> wrapper = new QueryWrapper<EduClasss>();
		String name = query.getName();
		String begin=query.getBegin();
		String end = query.getEnd();
		if(!StringUtils.checkValNull(name)) {
			wrapper.like("name", name);//模糊查询
		}
		if(!StringUtils.checkValNull(begin)) {
			wrapper.ge("gmt_create", begin);
		}
		if(!StringUtils.checkValNull(end)) {
			wrapper.le("gmt_create", end);
		}
		classsService.page(page, wrapper);
		List<EduClasss> records = page.getRecords();
		long total = page.getTotal();
		return R.ok().data("total", total).data("rows", records);
	}
	@GetMapping("/getAllByMajorId/{majorId}")
	public R getClassByMajId(@PathVariable String majorId) {
		QueryWrapper<EduClasss> queryWrapper=new QueryWrapper<EduClasss>();
		queryWrapper.eq("major_id",majorId);
		List<EduClasss> list = classsService.list(queryWrapper);
		return R.ok().data("classs",list);
	}
	@GetMapping("/getClassByTeacherId/{teacherid}")
	public R GetClasssListByTeacher(@PathVariable(value="teacherid") String teacherid) {
		List<ClassListVo> classByTeacher = classsService.getClassByTeacher(teacherid);
		for(ClassListVo i:classByTeacher) {
			System.out.println(i.toString());
		}
		return R.ok().data("classs",classByTeacher);
	}
	
}

